package com.ruoyi.project.blog.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.FieldConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.blog.domain.BCategory;
import com.ruoyi.project.blog.domain.vo.SystemCategoryListVO;
import com.ruoyi.project.blog.mapper.BCategoryMapper;
import com.ruoyi.project.blog.service.IBCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.enums.ResultCode.CATEGORY_IS_EXIST;

/**
 * 博客分类Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-19
 */
@Service
public class BCategoryServiceImpl extends ServiceImpl<BCategoryMapper, BCategory> implements IBCategoryService {
    @Autowired
    private BCategoryMapper bCategoryMapper;

    /**
     * 查询博客分类
     *
     * @param id 博客分类主键
     * @return 博客分类
     */
    @Override
    public BCategory selectBCategoryById(Long id) {
        return bCategoryMapper.selectBCategoryById(id);
    }

    /**
     * 查询博客分类列表
     *
     * @param bCategory 博客分类
     * @return 博客分类
     */
    @Override
    public List<SystemCategoryListVO> selectBCategoryList(BCategory bCategory) {
        return bCategoryMapper.selectBCategoryList(bCategory);
    }

    /**
     * 新增博客分类
     *
     * @param bCategory 博客分类
     * @return 结果
     */
    @Override
    public AjaxResult insertBCategory(BCategory bCategory) {
        bCategory.setCreateTime(DateUtils.getNowDate());
        BCategory vo = baseMapper.selectOne(new QueryWrapper<BCategory>().eq(FieldConstants.NAME, bCategory.getName()));
        if (vo != null) {
            throw new BusinessException(CATEGORY_IS_EXIST);
        }
        int rows = bCategoryMapper.insertBCategory(bCategory);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("添加分类失败");
    }

    /**
     * 修改博客分类
     *
     * @param bCategory 博客分类
     * @return 结果
     */
    @Override
    public AjaxResult updateBCategory(BCategory bCategory) {
        bCategory.setUpdateTime(DateUtils.getNowDate());
        BCategory vo = baseMapper.selectOne(new QueryWrapper<BCategory>().eq(FieldConstants.NAME, bCategory.getName()));
        if (vo != null && !vo.getId().equals(bCategory.getId())) {
            throw new BusinessException(CATEGORY_IS_EXIST);
        }

        int rows = baseMapper.updateById(bCategory);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("修改分类失败");
    }

    /**
     * 批量删除博客分类
     *
     * @param ids 需要删除的博客分类主键
     * @return 结果
     */
    @Override
    public int deleteBCategoryByIds(Long[] ids) {
        return bCategoryMapper.deleteBCategoryByIds(ids);
    }

    /**
     * 删除博客分类信息
     *
     * @param id 博客分类主键
     * @return 结果
     */
    @Override
    public int deleteBCategoryById(Long id) {
        return bCategoryMapper.deleteBCategoryById(id);
    }

    @Override
    public List<SystemCategoryListVO> listTags(String name) {
        List<SystemCategoryListVO> list = bCategoryMapper.listTags(name);
        return list;
    }

    @Override
    public ResponseResult selectCategoryList(Long userId) {
        LambdaQueryWrapper<BCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(BCategory::getUserId, userId);
        }
        queryWrapper.orderByDesc(BCategory::getSort);
        List<BCategory> list = bCategoryMapper.selectList(queryWrapper);
        return ResponseResult.success(list);
    }
}
